<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Euclidean projection on the semidefinite cone</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/cvxbook/Ch08_geometric_probs/html/eucl_proj_cone2.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Euclidean projection on the semidefinite cone</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Sec. 8.1.1, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Joelle Skaf - 10/07/05</span>
<span class="comment">%</span>
<span class="comment">% The projection of X0 on the proper cone K = S+^n is given by</span>
<span class="comment">%           minimize    ||X - X0||_F</span>
<span class="comment">%               s.t.    X &gt;=0</span>
<span class="comment">% where X is a nxn matrix and ||.||_F is the Frobenius norm</span>
<span class="comment">% It is also given by: P_K(X0)_k = sum_{i=1}^{n}max{0,lam_i}v_i*v_i'</span>
<span class="comment">% s.t. X0= sum_{i=1}^{n}lam_i*v_i*v_i'is the eigenvalue decomposition of X0</span>

<span class="comment">% Input data</span>
randn(<span class="string">'seed'</span>,0);
n  = 10;
X0 = randn(n);
X0 = 0.5 * (X0 + X0');
[V,lam] = eig(X0);

fprintf(1,<span class="string">'Computing the analytical solution...'</span>);
<span class="comment">% Analytical solution</span>
pk_X0 = V*max(lam,0)*V';
fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Solution via CVX</span>
fprintf(1,<span class="string">'Computing the optimal solution by solving an SDP...'</span>);

cvx_begin <span class="string">sdp</span> <span class="string">quiet</span>
    variable <span class="string">X(n,n)</span> <span class="string">symmetric</span>
    minimize ( norm(X-X0,<span class="string">'fro'</span>) )
    X &gt;= 0;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Verification</span>
disp(<span class="string">'-----------------------------------------------------------------'</span>);
disp(<span class="string">'Verifying that the analytical solution and the solution obtained '</span>);
disp(<span class="string">'via CVX are equal by computing ||X_star - P_K(X0)||_F: '</span>);
norm(X-pk_X0,<span class="string">'fro'</span>)
disp(<span class="string">'Hence X_star and P_K(X0) are equal to working precision.'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing the analytical solution...Done! 
Computing the optimal solution by solving an SDP...Done! 
-----------------------------------------------------------------
Verifying that the analytical solution and the solution obtained 
via CVX are equal by computing ||X_star - P_K(X0)||_F: 

ans =

   2.5435e-04

Hence X_star and P_K(X0) are equal to working precision.
</pre>
</div>
</body>
</html>